/*
 * JimBot - Java IM Bot
 * Copyright (C) 2006-2010 JimBot project
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package faac.http.MainPage;

import org.eclipse.jetty.http.security.Constraint;
import org.eclipse.jetty.http.security.Password;
import org.eclipse.jetty.security.ConstraintMapping;
import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.security.HashLoginService;
import org.eclipse.jetty.security.authentication.FormAuthenticator;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import faac.util.MainProps;




/**
 * Определяет из доступных скриптов и создает новые обработчики http-запросов
 *
 * @author Prolubnikov Dmitry
 */
public class HandlerFactory {

    /**
     * Создает и возвращает список доступных (установленных в боте) обработчиков
     * @return
     */
    public static HandlerList getAvailableHandlers() {
        HandlerList handlers = new HandlerList();
        // Добавляем обработчик файлов
        ResourceHandler resource_handler = new ResourceHandler();
        resource_handler.setDirectoriesListed(true);
        resource_handler.setWelcomeFiles(new String[]{ "index.html" });
        resource_handler.setResourceBase("./HTML");
        // Авторизация
        Constraint constraint = new Constraint();
        constraint.setName("user");
        constraint.setRoles(new String[]{"user","admin"});
        
        constraint.setAuthenticate(true);
        ConstraintMapping cm = new ConstraintMapping();
        cm.setConstraint(constraint);
        cm.setPathSpec("/*");






        ConstraintSecurityHandler sh = new ConstraintSecurityHandler();
        HashLoginService ls = new HashLoginService("JimBotRealm");
        ls.putUser(MainProps.getStringProperty("http.user"), new Password(MainProps.getStringProperty("http.pass")), new String[]{"admin","user"});
//ls.putUser("DIGGER",new Password("DIGGER"), new String[]{"user","admin"});
        FormAuthenticator auth = new FormAuthenticator("/logon.html", "/logonError.html", false);
        sh.setAuthenticator(auth);
        sh.setAuthMethod("FORM");
        
        sh.setLoginService(ls);
        sh.setConstraintMappings(new ConstraintMapping[]{cm});














        //Обработчики сервлетов
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
//        context.addServlet(new ServletHolder(new TestServlet()),"/test");
//        context.addServlet(new ServletHolder(new TestServlet2()),"/test2");
//       context1.addServlet(new ServletHolder(new HTTPScriptRequest()),"/");
        context.addServlet(new ServletHolder(new MainPageServlet()),"/main");
        context.addServlet(new ServletHolder(new MainPageServlet()),"/j_security_check");
       context.setSecurityHandler(sh);
        context.getSessionHandler().getSessionManager().setMaxInactiveInterval(MainProps.getIntProperty("http.delay")*60);
        handlers.setHandlers(new Handler[] {resource_handler, context, new DefaultHandler() });
        return handlers;
    }
}
